﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Serious
{
    public class Cylinder
    {
        public static Single gravity = 0.05f;

        public Vector3 position;
        public Single angle;
        
        public Vector3 velocity;
        public Single angle_delta;

        public Vector2 move_vector;

        public Single radius;
        public Single half_height;

        public bool on_floor;

        public BasicModel model;

        public Matrix transformation
        {
            get
            {
                return Matrix.CreateRotationZ(MathHelper.ToRadians(angle)) *
                       Matrix.CreateTranslation(position);
            }
        }

        public Cylinder (Vector3 position, Single angle, Single radius, Single half_height)
        {
            this.position = position;
            this.angle = angle;

            this.velocity = Vector3.Zero;
            this.angle_delta = 0.0f;

            this.move_vector = Vector2.Zero;

            this.radius = radius;
            this.half_height = half_height;

            on_floor = false;

            this.model = BasicModel.GenerateColoredCylinderModel(24, radius, half_height, Color.Green);
        }

        public Cylinder cloned ()
        {
            return new Cylinder(position, angle, radius, half_height);
        }
        public void set(Cylinder other)
        {
            this.position = other.position;
            this.angle = other.angle;
            this.radius = other.radius;
            this.half_height = other.half_height;
        }

        public void Update (GameTime gameTime)
        {
            velocity.Z -= Cylinder.gravity;

            angle += angle_delta;
            angle = MathExt.WrapAngleDeg(angle);
            Vector2 vel = move_vector.Rotated(Vector2Ext.FromDirectionDeg(angle));
            velocity.X = vel.X;
            velocity.Y = vel.Y;
            position += velocity;
        }

        public void Draw(GameTime gameTime, GraphicsDevice device)
        {
            model.Draw(device);
        }
    }
}
